%%%-------------------------------------------------------------------
%%% @author mirahs(2892727493@qq.com)
%%% @doc
%%%     角色基础数据
%%% @end
%%%-------------------------------------------------------------------
-module(role_base_parse).

-export([parse/1, to_role/1, to_role_base/1]).

-export([get_ver/1, get_ver_index/0, validate/1, get_field_parse/0, process_after/1]).

-include("common.hrl").
-include("role_data.hrl").
-include("parse.hrl").
-include("role.hrl").


parse(D) ->
    parse:parse(?MODULE, D).

%% 将 role_base 转换成 role
to_role(RoleBase) ->
    case parse(RoleBase) of
        {ok, #role_base{
            rid = Rid, sid = Sid, account = Account, name = Name, sex = Sex, label = Label, lev = Lev
            ,status = Status,online_time = OnlineTime, day_online = DayOnline, last_charge_time = LastChargeTime
            ,reg_time = RegTime, reg_ip = RegIp, login_time = LoginTime, login_ip = LoginIp, logout_time = LogoutTime, last_login = LastLogin, last_logout = LastLogout
            ,exp = Exp, gold = Gold, gold_bind = GoldBind,charge = Charge, gold_consume = GoldConsume
        }} ->
            Assets = #assets{exp = Exp, gold = Gold, gold_bind = GoldBind,charge = Charge, gold_consume = GoldConsume},
            LoginInfo = #login_info{
                reg_time = RegTime, reg_ip = RegIp, login_time = LoginTime, login_ip = LoginIp, logout_time = LogoutTime
                ,last_login = LastLogin, last_logout = LastLogout},

            Role = #role{
                rid = Rid, sid = Sid, account = Account, name = Name, sex = Sex, label = Label, lev = Lev
                ,status = Status,online_time = OnlineTime, day_online = DayOnline, last_charge_time = LastChargeTime
                ,login_info = LoginInfo, assets = Assets
            },
            {ok, Role};
        {false, Reason} ->
            {false, Reason}
    end.

%% 将 role 转换成 role_base
to_role_base(#role{
    rid = Rid, sid = Sid, account = Account, name = Name, sex = Sex, label = Label, lev = Lev
    ,status = Status,online_time = OnlineTime, day_online = DayOnline, last_charge_time = LastChargeTime
    ,login_info = LoginInfo,assets = Assets
    ,is_online = IsOnline
}) ->
    #assets{exp = Exp, gold = Gold, gold_bind = GoldBind,charge = Charge, gold_consume = GoldConsume} = Assets,
    #login_info{reg_time = RegTime, reg_ip = RegIp, login_time = LoginTime, login_ip = LoginIp, logout_time = LogoutTime
        ,last_login = LastLogin, last_logout = LastLogout} = LoginInfo,

    RoleBase = #role_base{
        rid = Rid, sid = Sid, account = Account, name = Name, sex = Sex, label = Label, lev = Lev
        ,status = Status,online_time = OnlineTime, day_online = DayOnline, last_charge_time = LastChargeTime
        ,reg_time = RegTime, reg_ip = RegIp, login_time = LoginTime, login_ip = LoginIp, logout_time = LogoutTime, last_login = LastLogin, last_logout = LastLogout
        ,exp = Exp, gold = Gold, gold_bind = GoldBind,charge = Charge, gold_consume = GoldConsume
        ,is_online = IsOnline
    },
    {ok, RoleBase}.


%%%===================================================================
%%% parse behaviour
%%%===================================================================

%% @spec get_ver_index() -> integer()
%% 返回版本号的索引
get_ver_index() ->
    #role_base.ver.

%% @spec validate(Data) -> true | false
%% Data = tuple()
%% 检测数据的正确性
validate(Data) ->
    is_record(Data, role_base).

%% @spec get_field_parse() -> list()
%% 返回子元素的解析
get_field_parse() ->
    [].

%% @spec process_after(Data) -> NewData
%% Data = NewData = tuple()
%% 解析后的数据再处理
process_after(Data) ->
    {ok, Data}.

%% get_ver(Ver) -> ok | #parse_ver{}
%% Ver = integer()
%% 返回每个版本的变动，最终版本返回ok
get_ver(?role_base_ver) ->
    ok.
